/*
 * visualize_topology.cpp
 *
 *  Created on: May 24, 2011
 *      Author:
 *     Alper Aydemir
 */


#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <ticpp.h>
#include <vector>
#include <map>
#include "Node.h"
#include "cgraph.h"

// simple function to parse a xml file
void Parse(const char* pcFilename);

std::map<std::string, Node> nodes; // id and node itself, granted this is kinda stupid

int main() {
	// whatch out for exceptions
	Parse("../MITData/maps.csail.mit.edu/corpus/32-1.xml");

	return -1;
}

void Parse(const char* pcFilename) {
	// first load the xml file
	ticpp::Document doc(pcFilename);
	doc.LoadFile();


	ticpp::Iterator<ticpp::Element> child;
	Node tmpnode;
	// First iterate through all nodes
	for (child = child.begin(doc.FirstChildElement()); child != child.end(); child++) {
		// The value of this child identifies the name of this element
		std::string strName;
		std::string spaceType;
		child->GetValue(&strName);

		if (strName == "space") {

			std::string spaceName, spaceType;
			child->ToElement()->GetAttribute("name", &spaceName);
			child->ToElement()->GetAttribute("type", &spaceType);
			std::cout << "name: " << spaceName << " type: " << spaceType
					<< std::endl;
			tmpnode._id = spaceName;
			tmpnode._spaceType = spaceType;
			nodes[spaceName] = tmpnode;
		}
	}


	printf("Found %d portal nodes \n", nodes.size());
	std::map<std::string,Node>::iterator it;

	for (child = child.begin(doc.FirstChildElement()); child != child.end(); child++) {
		std::string strName;
		child->GetValue(&strName);
		if (strName == "space") {
			std::string spaceName;
			child->ToElement()->GetAttribute("name", &spaceName);

			ticpp::Node* spaceChildren;
			spaceChildren = child->FirstChild("portal", false);
			ticpp::Node* nodeit;
			while (spaceChildren) {
				try {
					std::string tmp, portalTarget;
					spaceChildren->GetValue(&tmp);
					if (tmp == "portal") {
						spaceChildren->ToElement()->GetAttribute("target",
								&portalTarget);
						std::cout << "portalTarget: " << portalTarget
								<< std::endl;
						it = nodes.find(portalTarget);
						if (it != nodes.end()){
							nodes[spaceName].connections.push_back(nodes[portalTarget]);
							nodes[portalTarget].connections.push_back(nodes[spaceName]);

						}
					}
					spaceChildren = spaceChildren->NextSibling("portal", false);
				} catch (ticpp::Exception& ex) {
					std::cout << ex.what() << std::endl;
					break;
				}
			}
		}
	}


// Construct a DOT graph programmatically (note quite hacky code)
	Agraph_t* graph;
	graph = agopen("test", Agundirected,0);

	std::map<std::string, Agnode_t*> graphNodes;

	std::map<std::string,Node>::iterator it2;
	for (it2=nodes.begin(); it2 != nodes.end(); it2++){
		std::cout << "writing space type: " << it2->second._spaceType << std::endl;
		std::string tmp = it2->first;
		graphNodes[tmp] = agnode(graph,&it2->second._spaceType[0], true);
	}
std::cout << __LINE__ << std::endl;
	for (it2=nodes.begin(); it2 != nodes.end(); it2++){
		for (int i=0; i < it2->second.connections.size(); i++){
			std::cout << __LINE__ << std::endl;
			std::string from = it2->first;
			std::cout << __LINE__ << std::endl;
			std::string to = it2->second.connections[i]._id;
			std::cout << __LINE__ << std::endl;
			agedge(graph,graphNodes[from], graphNodes[to],"", true);
			std::cout << __LINE__ << std::endl;
		}
	}
	std::cout << __LINE__ << std::endl;
	std::cout << "writing to file" << std::endl;
	freopen ("graph_second.dot","w",stdout);
	agwrite(graph, stdout);
		fclose (stdout);
		std::cout << __LINE__ << std::endl;
		if (system(NULL)) puts ("Ok");
		    else exit (1);
		  printf ("Executing command DIR...\n");
		  int i=system ("dot -Tps graph_second.dot -o graph_second.pdf");
		  printf ("The value returned was: %d.\n",i);
}
